package com.maoye.ossp.biz.intent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.entity.AssociatedContract;
import com.maoye.ossp.biz.contract.entity.MarketAssociated;
import com.maoye.ossp.biz.contract.service.IMarketAssociatedService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAssterminate;
import com.maoye.ossp.biz.intent.entity.IntMarketterminate;
import com.maoye.ossp.biz.intent.service.IntMarketterminateService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:促销撤柜意向  控制器类
 * 开发公司:MOI
 * 开发人员:zhongjt
 * 创建时间:2014-02-20 14:50:38
 */
@Controller
@RequestMapping("biz/intent/intMarketterminate")
public class IntMarketterminateController extends BaseController {
	@Resource
	private IntMarketterminateService intMarketterminateService;
	@Resource
	private IMarketAssociatedService  contractServce;
	
	@RequestMapping(value = "list")
	public String list(IntMarketterminate intMarketterminate,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		QueryFilter queryFilter = getQuerFilter(request,intMarketterminate);
		queryFilter.addFilter("areaId", intMarketterminate.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intMarketterminate.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intMarketterminate.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("startDate", intMarketterminate.getStartDate(), QueryOP.EQUAL);
		queryFilter.addFilter("endDate", intMarketterminate.getEndDate(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierName", intMarketterminate.getSupplierName(), QueryOP.EQUAL);
		if(intMarketterminate.getIntentNo()!=null){
			queryFilter.addFilter("intentNo", intMarketterminate.getIntentNo(), QueryOP.LIKE);
		}
		String createBy = null;
		if(StringUtil.isNotEmpty(intMarketterminate.getCreateBy())){
			createBy = ContractUtils.getUserId(intMarketterminate.getCreateBy());
		}
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		Page<IntMarketterminate> page = (Page<IntMarketterminate>) intMarketterminateService.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("intMarketterminate", intMarketterminate);
		return resultPage("/biz/intent/intMarketterminate/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntMarketterminate intMarketterminate, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intMarketterminateService.save(intMarketterminate);
		addMessage(redirectAttributes,
				"保存'" + intMarketterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intMarketterminate/list?navId="+navId;
	}
	
	@RequestMapping(value = "submit")
	public String submit(IntMarketterminate intAssterminate, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intMarketterminateService.submit(intAssterminate);
		addMessage(redirectAttributes,
				"提交'" + intAssterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intMarketterminate/list?navId="+navId;
	}

	@RequestMapping(value = "{contractId}/add")
	public String add( @PathVariable Long contractId,Model model,HttpServletRequest request) {
		model = intMarketterminateService.init(contractId, model);
		return resultPage("/biz/intent/intMarketterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntMarketterminate intMarketterminate = intMarketterminateService.get(id);
		MarketAssociated associatedContract = contractServce.get(intMarketterminate.getContractId().toString());
		model.addAttribute("intMarketterminate", intMarketterminate);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intMarketterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntMarketterminate intMarketterminate = intMarketterminateService.get(id);
		model.addAttribute("intMarketterminate", intMarketterminate);
		return resultPage("/biz/intent/intMarketterminate/view", model, request);
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		intMarketterminateService.remove(id);
		addMessage(redirectAttributes, "删除撤柜意向成功！");
		return "redirect:/biz/intent/intMarketterminate/list";
	}

	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String item = intMarketterminateService.removeBatch(ids);
		String[] intentNos = item.split(",");
		if(!"".equals(item)){
			addMessage(redirectAttributes,"意向单号为"+item+"的意向已经进入流程审批，不能被删除！");
		}else{
			addMessage(redirectAttributes,"删除撤柜意向成功");
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/intent/intMarketterminate/list?navId="+navId;
	}
	
	/**
	 * 获取合同信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(AssociatedContract associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) {
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		Page<MarketAssociated> page = contractServce.find(queryFilter);
		int totalSize = (page.getTotal() + page.getPageSize() - 1) / page.getPageSize();
		model.addAttribute("page", page);
		model.addAttribute("totalSize", totalSize);
		return resultPage("/biz/intent/common/modal", model, request);
	}
	/**
	 * 获取合同信息(分页)
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @throws IOException 
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractListPage")
	public void getModuleContractListPage(AssociatedContract associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) throws IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		String supplierName =associatedContract.getSupplierName();
		String contractNo = associatedContract.getContractNo();
		String subject = associatedContract.getSubject();
		queryFilter.addFilter("supplierName", supplierName!=""?supplierName:null, QueryOP.LIKE);
		queryFilter.addFilter("contractNo", contractNo !=""?contractNo:null, QueryOP.LIKE);
		queryFilter.addFilter("subject", subject!=""?subject:null, QueryOP.LIKE);
		Page<MarketAssociated> page = (Page<MarketAssociated>) contractServce.find(queryFilter);
		List<MarketAssociated> list = page.getList();
		
		StringBuffer sb = new StringBuffer(); 
		sb.append("{\"total\":\""+page.getTotal()+"\",");
		sb.append("\"pageNo\":\""+page.getPageNo()+"\",");
		sb.append("\"pageSize\":\""+page.getPageSize()+"\",");
		sb.append("\"jsonRoot\":[");
		
		for(int i =0;i<list.size();i++){
			MarketAssociated  contract = list.get(i);
			sb.append("{\"id\":\""+ contract.getId());  
            sb.append("\",");
            sb.append("\"supplierName\":\""+contract.getSupplierName()+"\",");
            sb.append("\"subject\":\""+contract.getSubject()+"\",");
            sb.append("\"contractNo\":\""+contract.getContractNo()+"\"");
			sb.append("},");
		}
		if(list.size()>0){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		sb.append("]}"); 
        out.print(sb);
        out.flush();
		out.close();
	}
}
